package com.yumei.trader.gym.view.element;

import android.annotation.SuppressLint;

import org.json.JSONArray;
import org.json.JSONObject;


/**
 * 股票盘口对像
 */
public class StockElement extends TicketElement {
    
    public String stockcode; //代码
    public String stockname; //名称

    public String date; //日期
    public String time="0000"; //时间

    public float prvClose; //昨收
    public float gain; // 涨幅

    public double[] buyPrice = new double[5];
    public double[] buyVolume = new double[5];

    public double[] sellPrice = new double[5];
    public double[] sellVolume = new double[5];

    //排序
    public String sequence;
    public boolean isChange = false;
    public String robotStatus = "";

    @SuppressLint("DefaultLocale")
    @Override
    public void parseJson(JSONObject jsonObj) throws Exception {

        super.parseJson(jsonObj);

        stockcode = jsonObj.optString("itemCode");

        stockname = jsonObj.optString("stockname");

        date = jsonObj.optString("date","0");

        int timeInt  = jsonObj.optInt("time",0);


        if(timeInt >113000 && timeInt <130000){
            timeInt = 113000;
        }

        if(timeInt >150000){
            timeInt = 150000;
        }


        time = String.format("%04d", timeInt/100);


        String prvCloseStr = jsonObj.optString("prvClose","0");
        prvClose = Float.parseFloat(prvCloseStr);

        JSONArray buyPriceArr = jsonObj.optJSONArray("buyPrice");
        if(buyPriceArr != null){
            for (int i = 0; i < buyPriceArr.length(); i++) {
                buyPrice[i] = (double) buyPriceArr.get(i);
            }
        }

        JSONArray buyVolumeArr = jsonObj.optJSONArray("buyVolume");
        if(buyVolumeArr != null){
            for (int i = 0; i < buyVolumeArr.length(); i++) {
                buyVolume[i] = (double) buyVolumeArr.get(i);
            }
        }

        JSONArray sellPriceArr = jsonObj.optJSONArray("sellPrice");
        if(sellPriceArr != null){
            for (int i = 0; i < sellPriceArr.length(); i++) {
                sellPrice[i] = (double) sellPriceArr.get(i);
            }
        }

        JSONArray sellVolumeArr = jsonObj.optJSONArray("sellVolume");
        if(sellVolumeArr != null){
            for (int i = 0; i < sellVolumeArr.length(); i++) {
                sellVolume[i] = (double) sellVolumeArr.get(i);
            }
        }

        sequence = jsonObj.optString("sequence","0");
    }





    public void parseStockJson(JSONObject jsonObj) {

        stockcode = jsonObj.optString("symbol");
        stockname = jsonObj.optString("stockName");

        robotStatus = jsonObj.optString("identification", "");
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof StockElement){
            StockElement element = (StockElement) obj;
            return stockcode.equals(element.stockcode);
        }else {
            return super.equals(obj);
        }
    }


    public String toString(){
        return  date+"\t"+time+"\t"+high+"\t"+low+"\t"+close;
    }
}
